import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      meta: {
        requestAuth: false,
        flesh: false,
      },
      component: () => import('@/views/login/LoginView.vue')
    },
    {
      path: '/guide',
      meta: {
        requestAuth: true,
        flesh: true,
      },
      component: () => import('@/views/guide/GuideView.vue')
    },
    {
      path: '/rules',
      meta: {
        requestAuth: false,
        flesh: false,
      },
      component: () => import('@/components/UserRules.vue')
    },
    {
      path: '/policy',
      meta: {
        requestAuth: false,
        flesh: false,
      },
      component: () => import('@/components/UserPolicy.vue')
    },
    {
      path: '/',
      meta: {
        requestAuth: true,
        flesh: false,
      },
      component: () => import('@/views/layout/LayOutContainer.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          meta: {
            requestAuth: true,
            flesh: false,
          },
          component: () => import('@/views/home/HomeView.vue')
        },
        {
          path: '/select/job',
          meta: {
            requestAuth: true,
            flesh: false,
          },
          component: () => import('@/views/select/SelectJob.vue')
        },
        {
          path: '/select/company',
          meta: {
            requestAuth: true,
            flesh: false,
          },
          component: () => import('@/views/select/SelectCompany.vue')
        },
        {
          path: '/talk/chat',
          meta: {
            requestAuth: true,
            flesh: false,
          },
          component: () => import('@/views/talk/ChatOther.vue')
        },
        {
          path: '/user/info',
          meta: {
            requestAuth: true,
            flesh: false,
          },
          component: () => import('@/views/user/UserInfo.vue')
        },
        {
          path: '/data/analysis',
          meta: {
            requestAuth: true,
            flesh: false,
          },
          component: () => import('@/views/data/DataAnalysis.vue')
        }
      ]
    }
  ]
})
const userStore = useUserStore()
router.beforeEach((to, from, next) => {
  if (to.meta.requestAuth && !userStore.is_login) {
    next({ path: '/login' })
  } else if (to.meta.requestAuth && userStore.is_login) {
    if(!to.meta.flesh){
      if(userStore.user.expectJob && userStore.user.expectJob !== null){
        next();
      }
      else{
        next({path: '/guide'})
      }
    }else {
      if(userStore.user.expectJob && userStore.user.expectJob !== null){
        next({push: '/'})
      }else{
        next();
      }
    }
  }else if(!to.meta.requestAuth && userStore.is_login){
    next({ push: '/'})
  }else{
    next()
  }
})
export default router
